package com.ustadzmenjawab.indexing;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Hashtable;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;

import com.ustadzmenjawab.server.DBConnFactory;



public class LuceneIndexing {
	private IDBAccess db;
	private IndexWriter writer;
	Directory fsdir;
	public static final String INDEX_LOC = "war/WEB-INF/index";
	static final int MAX_HITS = 10;
	public static void main(String[] args){
		String indexLocArg = args[0];
		if(indexLocArg == null || indexLocArg.length() == 0){
			System.out.println("Index location is not specified in args[0].");
			return;
		}
		LuceneIndexing lucene = new LuceneIndexing(indexLocArg);
		lucene.indexAllRecords();
	}
	public LuceneIndexing(String indexLoc){
		if(indexLoc == null)
			return;
		db = new IDBAccess();
		
		String luceneIndexLoc = indexLoc;
		try {
			Directory fsdir = new NIOFSDirectory(new File(luceneIndexLoc));
			//Analyzer inaAnalyzer = new IndonesianAnalyzer(Version.LUCENE_35);
			UstadzMenjawabSynAnalyzer analyzer = new UstadzMenjawabSynAnalyzer(Version.LUCENE_35);
			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);			
			//	config.setSimilarity(new CustomSimilarity());
			config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
		
			writer = new IndexWriter(fsdir, config);
	 
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void indexAllRecords(){
		Connection conn = DBConnFactory.getConnectionWODataSource();
		String sql = "Select * From ustadzmenjawabdb_prod WHERE offline != 'D'";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery(sql);
			ResultSetMetaData metaData = rs.getMetaData();
			int rowCount = metaData.getColumnCount();
			while(rs.next()){
				  Hashtable<String,String> rec = new Hashtable<String,String>();
				  for (int i = 0; i < rowCount; i++) {
					  String colName = metaData.getColumnName(i + 1);
					  String val = rs.getString(colName);
					  rec.put(colName, val);
				  }
				  indexOneRecords(rec);
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void indexOneRecords(Hashtable<String,String> rec) throws CorruptIndexException, IOException{
		Document doc = new Document();
		for(String key:rec.keySet()){
			Field f = new Field(key,rec.get(key),Field.Store.YES, Field.Index.ANALYZED);
			doc.add(f);
		}
		writer.addDocument(doc);
		writer.commit();
		System.out.println("index one record.");
	}
	
	public void closeWriter(){
		try {
			writer.close(true);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
